<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="考试项目" prop="projectCode">
        <el-select v-model="queryParams.projectCode" placeholder="请选择考试项目" style="width: 180px;">
          <el-option
            v-for="dict in projectList"
            :key="dict.code"
            :label="dict.name"
            :value="dict.code"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="规则名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入规则名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择性别" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属区域" prop="areaCode">
        <el-select v-model="queryParams.areaCode" placeholder="请选择所属区域" style="width: 180px;">
          <el-option
            v-for="dict in areaList"
            :key="dict.v"
            :value="dict.v"
            :label="dict.n">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年份" prop="year">
        <el-select v-model="queryParams.year" placeholder="请选择年份" style="width: 180px;">
          <el-option
            v-for="dict in yearList"
            :key="dict"
            :label="dict"
            :value="dict">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ruleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="考试项目" align="center" prop="projectName"/>
      <el-table-column label="规则名称" align="center" prop="name"/>
      <el-table-column label="填写说明" align="center" prop="explain"/>
      <el-table-column label="规则范围1" align="center" prop="num1" :formatter="rounding"/>
      <el-table-column label="规则范围2" align="center" prop="num2" :formatter="rounding"/>
      <el-table-column label="单位" align="center" prop="unit"/>
      <el-table-column label="得分" align="center" prop="score"/>
      <el-table-column label="是否启用" align="center" prop="isUse">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_use" :value="scope.row.isUse"/>
        </template>
      </el-table-column>
      <el-table-column label="是否特殊规则" align="center" prop="isSpecial">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_special" :value="scope.row.isSpecial"/>
        </template>
      </el-table-column>
      <el-table-column label="特殊规则码" align="center" prop="specialCode">
        <template slot-scope="scope">
          <dict-tag-remark :options="dict.type.specialTypes" :value="scope.row.specialCode"/>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考点评分规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="规则名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入规则名称"/>
        </el-form-item>
        <el-form-item label="考试项目" prop="projectCode">
          <el-select v-model="form.projectCode" placeholder="请选择考试项目" style="width: 180px;" @change="projectChange">
            <el-option
              v-for="dict in projectList"
              :key="dict.code"
              :label="dict.name"
              :value="dict.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="填写说明" prop="explain">
          <el-input v-model="form.explain" style="width: 485px" type="textarea" placeholder="请输入填写说明"/>
        </el-form-item>
        <el-form-item label="规则范围1" prop="num1">
          <el-input-number :precision="2" :step="0.01" v-model="form.num1" placeholder="请输入规则范围1"
                           style="width: 180px;"/>
        </el-form-item>
        <el-form-item label="规则范围2" prop="num2">
          <el-input-number :precision="2" :step="0.01" v-model="form.num2" placeholder="请输入规则范围2"
                           style="width: 180px;"/>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" disabled maxlength="2" placeholder="请输入单位"/>
        </el-form-item>
        <el-form-item label="得分" prop="score">
          <el-select v-model="form.score" placeholder="请选择得分" style="width: 180px;">
            <el-option
              v-for="dict in dict.type.exam_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否启用" prop="isUse">
          <el-select v-model="form.isUse" placeholder="请选择是否启用" style="width: 180px;">
            <el-option
              v-for="dict in dict.type.is_use"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择性别" style="width: 180px;">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否特殊规则">
          <el-select v-model="form.isSpecial" placeholder="请选择是否特殊规则" style="width: 180px;" @change="isSpecialChange">
            <el-option
              v-for="dict in dict.type.is_special"
              :key="dict.value"
              :value="parseInt(dict.value)"
              :label="dict.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="特殊规则码">
          <el-select v-model="form.specialCode" :disabled="specialDis" placeholder="请选择特殊规则码" style="width: 180px;">
            <el-option
              v-for="dict in dict.type.specialTypes"
              :key="dict.value"
              :value="parseInt(dict.value)"
              :label="dict.remark">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属区域" prop="areaCode">
          <el-select v-model="form.areaCode" placeholder="请选择所属区域" style="width: 180px;">
            <el-option
              v-for="dict in areaList"
              :key="dict.v"
              :value="dict.v"
              :label="dict.n">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年份" prop="year">
          <el-select v-model="form.year" placeholder="请选择年份" style="width: 180px;">
            <el-option
              v-for="dict in yearList"
              :key="dict"
              :label="dict"
              :value="dict">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" style="width: 485px" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRule,
  getRule,
  delRule,
  addRule,
  updateRule,
  exportRule,
  listYear,
  areaList,
  projectList
} from '@/api/business/rule/rule'
import { inputComponents } from '@/utils/generator/config'

export default {
  name: 'Rule',
  dicts: ['sys_user_sex', 'specialTypes', 'is_special', 'is_use', 'exam_result'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考点评分规则表格数据
      ruleList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectCode: null,
        name: null,
        num1: null,
        num2: null,
        unit: null,
        score: null,
        isSpecial: null,
        specialCode: null,
        gender: null,
        areaCode: null,
        year: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      yearList: [],
      areaList: [],
      projectList: [],
      specialDis: true
    }
  },
  created() {
    this.getList()
    this.getYearList()
    this.getAreaList()
    this.getProjectList()
  },
  methods: {
    /** 查询考点评分规则列表 */
    getList() {
      this.loading = true
      listRule(this.queryParams).then(response => {
        this.ruleList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getYearList() {
      let self = this
      listYear().then(response => {
        console.log(response)
        self.yearList = response.data
      })
    },
    getAreaList() {
      let self = this
      areaList().then(response => {
        console.log(response)
        self.areaList = response.data
      })
    },
    getProjectList() {
      let self = this
      projectList().then(response => {
        console.log(response)
        self.projectList = response.data
      })
    },
    projectChange(val) {
      let self = this
      var objVal = {}
      self.projectList.forEach((value) => {
        if (value.code == val) {
          objVal = value
        }
      })
      self.form.name = objVal.name
      self.form.explain = objVal.name
      self.form.projectName = objVal.name
      self.form.unit = objVal.unit
    },
    isSpecialChange(val) {
      let self = this
      self.form.specialCode = ''
      if (1 == val) {
        self.specialDis = false
      } else {
        self.specialDis = true
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectCode: null,
        projectName: null,
        name: null,
        explain: null,
        num1: null,
        num2: null,
        unit: null,
        score: null,
        remarks: null,
        isUse: null,
        isSpecial: [],
        specialCode: [],
        gender: null,
        areaCode: null,
        year: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加考点评分规则'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getRule(id).then(response => {
        this.form = response.data
        if (this.form.isSpecial == 1) {
          this.specialDis = false
        }
        this.open = true
        this.title = '修改考点评分规则'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRule(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addRule(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除考点评分规则编号为"' + ids + '"的数据项？').then(function() {
        return delRule(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal.confirm('是否确认导出所有考点评分规则数据项？').then(() => {
        this.exportLoading = true
        return exportRule(queryParams)
      }).then(response => {
        this.$download.name(response.msg)
        this.exportLoading = false
      }).catch(() => {
      })
    },
      rounding(row,column) {
          return parseFloat(row[column.property]).toFixed(2)
      },
  }
}
</script>
